package com.binvshe.binvshe.mine;


import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.binvshe.binvshe.R;
import com.binvshe.binvshe.common.http.MyHttpRequest;
import com.binvshe.binvshe.common.http.UrlCenter;
import com.binvshe.binvshe.mine.follow.bean.CoinBean;
import com.pachong.android.baseuicomponent.adapter.RecyclerViewBaseAdapter;
import com.pachong.android.baseuicomponent.fragment.SwipeListFragment;
import com.pachong.android.framework.httprequest.IHttpRequest;
import com.pachong.android.framework.httprequest.UrlParams;
import com.pachong.android.framework.httprequest.responsecallbacks.ListDataRequestListener;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * A simple {@link Fragment} subclass.
 */
public class CoinDetailFragment extends SwipeListFragment {
    private int mPage = 0;

    public CoinDetailFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        setPullToRefreshEnable(false);
        startLoading();
        return view;
    }

    @Override
    public RecyclerViewBaseAdapter createAdapter() {
        CoinListAdapter coinListAdapter = new CoinListAdapter(getActivity());
        return coinListAdapter;
    }

    private ListDataRequestListener<CoinBean> mResponceListener = new ListDataRequestListener<>(this, CoinBean.class);
    @Override
    public void onStartLoading() {
        if (mResponceListener.getResponseDataParser().hasMore()) {
            mPage++;
        }
        IHttpRequest request = new MyHttpRequest(getContext());
        UrlParams params = new UrlParams();
        params.put("pageNo", mPage + "");
        params.put("pageSize", 20 + "");
        request.get(UrlCenter.COIN_DETAIL, params, mResponceListener);
    }


    public class CoinListAdapter extends RecyclerViewBaseAdapter<CoinBean> {


        public CoinListAdapter(Context con) {
            super(con);
        }

        @Override
        public RecyclerView.ViewHolder onCreateHolder(ViewGroup parent, int viewType) {
            return new CoinListHolder(setAdapterViewItem(parent, R.layout.coin_item));
        }

        @Override
        public void onBindHolder(RecyclerView.ViewHolder holder, int position) {
            final CoinBean data = (CoinBean) getData().get(position);
            final CoinListHolder itemHolder = (CoinListHolder) holder;
            itemHolder.mTvCoinName.setText(data.getRemark());
            itemHolder.mTvCoinNum.setText(data.getAmount()+"");
            itemHolder.mTvCoinTime.setText(data.getTrade_time());
        }
        class CoinListHolder extends RecyclerView.ViewHolder {
            @Bind(R.id.tv_coin_name)
            TextView mTvCoinName;
            @Bind(R.id.tv_coin_time)
            TextView mTvCoinTime;
            @Bind(R.id.tv_coin_num)
            TextView mTvCoinNum;
            public CoinListHolder(View itemView) {
                super(itemView);
                ButterKnife.bind(this, itemView);
            }
        }
    }

}
